Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports System.Collections
Imports DotNetNuke.Security


Namespace WESNet.DNN.Modules.EPrayer
    Partial Class NewestPosts
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase

#Region "Private Members"
        Private _MasterModules As String()
        Private _MaxNewItems As Integer = 5
        Private _DaysForNew As Integer = 7
        Private _EnableLastVisited As Boolean = True
        Private _MaxBodyLength As Integer = 100
        Private _epc As EPrayerController
        Private _LoggedInUserId As Integer
        Private _IsAdministrator As Int16 = -2
#End Region

#Region "Protected Properties"
        Protected Property MaxNewItems() As Integer
            Get
                Return _MaxNewItems
            End Get
            Set(ByVal value As Integer)
                _MaxNewItems = value
            End Set
        End Property

        Protected Property MaxBodyLength() As Integer
            Get
                Return _MaxBodyLength
            End Get
            Set(ByVal value As Integer)
                _MaxBodyLength = value
            End Set
        End Property

        Protected Property DaysForNew() As Integer
            Get
                Return _DaysForNew
            End Get
            Set(ByVal value As Integer)
                _DaysForNew = value
            End Set
        End Property

        Protected Property EnableLastVisited() As Boolean
            Get
                Return _EnableLastVisited
            End Get
            Set(ByVal value As Boolean)
                _EnableLastVisited = value
            End Set
        End Property

        Protected ReadOnly Property LoggedInUserId() As Integer
            Get
                If EnableLastVisited Then
                    Return UserId
                Else
                    Return -1
                End If
            End Get
        End Property

        'Uses a tri-state check to avoid repeated calls to IsInRoles
        Public ReadOnly Property IsAdministrator() As Boolean
            Get
                If _IsAdministrator = -2 Then
                    '_IsAdministrator = Convert.ToInt16(PortalSecurity.IsInRoles(PortalSettings.ActiveTab.AdministratorRoles) _
                    '                                     Or PortalSecurity.IsInRoles(PortalSettings.AdministratorRoleName))
                    _IsAdministrator = Convert.ToInt16(MyBase.ModuleContext.EditMode Or PortalSecurity.IsInRoles(PortalSettings.AdministratorRoleName))
                End If
                Return Convert.ToBoolean(_IsAdministrator)
            End Get
        End Property

#End Region

#Region "Event Handlers"

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Settings("MaxNewItems") Is Nothing Then MaxNewItems = Convert.ToInt32(Settings("MaxNewItems"))
            If Not Settings("MaxBodyLength") Is Nothing Then MaxBodyLength = Convert.ToInt32(Settings("MaxBodyLength"))
            If Not Settings("DaysForNew") Is Nothing Then DaysForNew = Convert.ToInt32(Settings("DaysForNew"))
            If Not Settings("EnableLastVisited") Is Nothing Then EnableLastVisited = Convert.ToBoolean(Settings("EnableLastVisited"))

            Utilities.LocalizeGridView(gvNewRequests, LocalResourceFile)
            Utilities.LocalizeGridView(gvNewUpdates, LocalResourceFile)
            Utilities.LocalizeGridView(gvStats, LocalResourceFile)

            If Not IsPostBack Then
                _epc = New EPrayerController
                If Not Settings("ShowNewRequests") Is Nothing AndAlso Not Convert.ToBoolean(Settings("ShowNewRequests")) Then
                    tblShowNewRequests.Visible = False
                Else
                    BindNewRequests()
                End If
                If Not Settings("ShowNewUpdates") Is Nothing AndAlso Not Convert.ToBoolean(Settings("ShowNewUpdates")) Then
                    tblShowNewUpdates.Visible = False
                Else
                    BindNewUpdates()
                End If
                If Not Settings("ShowStats") Is Nothing AndAlso Not Convert.ToBoolean(Settings("ShowStats")) Then
                    tblShowStats.Visible = False
                Else
                    BindStats()
                End If
                If Not (tblShowNewRequests.Visible Or tblShowNewUpdates.Visible Or tblShowStats.Visible) Then
                    If IsAdministrator Then
                        Dim ContentPane As Control = ContainerControl.FindControl(DotNetNuke.Common.Globals.glbDefaultPane)
                        If Not ContentPane Is Nothing Then
                            CType(ContentPane, HtmlContainerControl).Style("border-top") = "2px #FF0000 solid"
                            CType(ContentPane, HtmlContainerControl).Style("border-bottom") = "2px #FF0000 solid"
                            CType(ContentPane, HtmlContainerControl).Style("border-right") = "2px #FF0000 solid"
                            CType(ContentPane, HtmlContainerControl).Style("border-left") = "2px #FF0000 solid"
                            DotNetNuke.UI.Skins.Skin.AddModuleMessage(Me, Localization.GetString("No_Content_Selected.Text", LocalResourceFile), _
                                                Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
                        End If
                    Else
                        ContainerControl.Visible = False
                    End If
                End If
            End If
        End Sub
#End Region

#Region "Protected Methods"
        Protected Function RequestTypeImageUrl(ByVal IsPraise As Boolean) As String
            If IsPraise Then
                Return ControlPath & "images/Praise.gif"
            Else
                Return ControlPath & "images/Prayer.gif"
            End If
        End Function

        Protected Function UpdateImageUrl(ByVal StatusID As Integer) As String
            Return ControlPath & "images/update" & StatusID.ToString("00") & ".gif"
        End Function
#End Region

#Region "Private Methods"
        Private Sub BindNewRequests()
            Dim Requests As Generic.List(Of NewPostInfo) = _epc.GetNewRequests(Me.ModuleId, DaysForNew, MaxNewItems, LoggedInUserId)
            If Requests.Count > 0 Then
                With gvNewRequests
                    .DataSource = Requests
                    .DataBind()
                End With
            Else
                gvNewRequests.Visible = False
                lblNoNewRequests.Visible = True
            End If
        End Sub

        Private Sub BindNewUpdates()
            Dim Updates As Generic.List(Of NewPostInfo) = _epc.GetNewUpdates(Me.ModuleId, DaysForNew, MaxNewItems, LoggedInUserId)
            If Updates.Count > 0 Then
                With gvNewUpdates
                    .DataSource = Updates
                    .DataBind()
                End With
            Else
                gvNewUpdates.Visible = False
                lblNoNewUpdates.Visible = True
            End If
        End Sub

        Private Sub BindStats()
            Dim SelectedMasterModules As Generic.List(Of MasterModuleInfo) = _epc.GetAvailableMasterModules(PortalId, Me.ModuleId)
            For i As Integer = SelectedMasterModules.Count - 1 To 0 Step -1
                If Not SelectedMasterModules(i).Selected Then SelectedMasterModules.RemoveAt(i)
            Next
            Dim SelectedModuleStats As New Generic.List(Of Stats)
            For Each m As MasterModuleInfo In SelectedMasterModules
                SelectedModuleStats.Add(_epc.GetStats(m.ModuleID, m.TabID))
            Next
            If SelectedModuleStats.Count > 0 Then
                With gvStats
                    .DataSource = SelectedModuleStats
                    .DataBind()
                End With
            Else
                tblShowStats.Visible = False
            End If

        End Sub
#End Region

#Region "Protected Methods"
        Protected Function PostURL(ByVal MasterModuleTabId As Integer, ByVal MasterModuleId As Integer, ByVal PostID As Integer) As String
            Return NavigateURL(MasterModuleTabId, "", "mid=" & MasterModuleId.ToString, "postid=" & PostID.ToString)
        End Function
#End Region

    End Class
End Namespace
